package com.twb.auth.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.twb.auth.entity.twbProject.TwbProject;
import com.twb.auth.entity.twbProject.dto.ListTwbProjectDto;
import com.twb.auth.entity.twbProject.dto.SearchTwbProjectDto;
import com.twb.auth.entity.twbProject.vo.ListTwbProjectVo;
import com.twb.auth.entity.twbProject.vo.SaveTwbProjectVo;
import com.twb.auth.entity.twbProject.vo.SearchTwbProjectVo;
import com.twb.auth.entity.twbProject.vo.UpdateTwbProjectByIdVo;
import com.twb.auth.mapper.TwbProjectMapper;
import com.twb.auth.service.ITwbProjectService;
import com.twb.core.base.entity.BaseLoginUser;
import com.twb.core.base.entity.TwbPageInfo;
import com.twb.core.base.service.impl.BaseService;
import com.twb.core.cache.redis.RedisUtil;
import com.twb.core.config.TwbConfigProperties;
import com.twb.core.exception.ServiceException;
import com.twb.core.util.BeanUtils;
import com.twb.core.util.DateUtil;
import com.twb.core.util.TwbPageUtil;
import com.twb.core.util.cipher.CipherUtil;

/**
 * @ClassName TwbProjectService
 * @Description 项目管理service
 * @author DSY
 * @date 2018年11月9日 上午9:42:02
 */
@Service("twbProjectService")
public class TwbProjectService extends BaseService<TwbProjectMapper,TwbProject> implements ITwbProjectService {

	@Autowired
	TwbProjectMapper twbProjectMapper;

	@Autowired
	private RedisUtil redisUtil;
	@Autowired
	private TwbConfigProperties twbConfigProperties;

	/**
	 * @Title getProject
	 * @Description 设置缓存前缀
	 * @return
	 * @AuthorOriginally DSY
	 * @date 2022年3月29日 下午4:04:23
	 */
	public String getProjectKey() {
		String key = this.twbConfigProperties.getProjectRedisKey();
		return key;
	}
	@Override
	public boolean initRedisProject() {
		boolean bl = false;

		TwbProject record = new TwbProject();
		record.setIsdel(0);
		List<TwbProject> queryForList = this.list(record);

		new TwbProject().sort(queryForList);

		redisUtil.set(getProjectKey(), queryForList);

		bl = true;
		return bl;
	}

	@Override
	public List<TwbProject> getRedisProject() {
		List<TwbProject> list = redisUtil.get(getProjectKey());
		if (null == list || list.isEmpty()) {
			boolean init = initRedisProject();
			if (init) {
				list = redisUtil.get(getProjectKey());
			}
		}
		return list;
	}

	@Override
	public TwbPageInfo<SearchTwbProjectDto> searchTwbProject(SearchTwbProjectVo vo) {
		TwbPageUtil.startPage(vo);
		List<SearchTwbProjectDto> list = twbProjectMapper.searchTwbProject(vo);

		return TwbPageUtil.of(list);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean saveTwbProject(SaveTwbProjectVo vo) {
		TwbProject record = new TwbProject();

		/*
		 * int count = 0; String app_id = ""; do { app_id =
		 * StringUtil.getRandomCode(8);// 获取8位随机码 record.setAppId(app_id); count =
		 * this.count(record); } while (count > 0);
		 */

		Integer maxSeq = twbProjectMapper.getMaxSeq();

		BeanUtils.copyNotNullProperties(vo, record);
		record.setId(vo.getCode().toUpperCase());
		record.setCode(vo.getCode().toUpperCase());
		record.setAppId(vo.getCode().toUpperCase());
		record.setAppToken(vo.getAppToken().toUpperCase());
		// 接入token签名+app_id+时间戳进行加密
		String app_secret = CipherUtil.SHAEncode(vo.getAppToken() + record.getAppId() + DateUtil.getNowStr_NONE_DATETIME());
		record.setAppSecret(app_secret);
		record.setStatus("1");
		record.setProSeq(maxSeq);
		this.setCreateInfo(record);
		boolean result = this.save(record);
		if (result) {
			initRedisProject();
		}
		return result;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean updateTwbProjectById(UpdateTwbProjectByIdVo vo) {
		TwbProject Project = twbProjectMapper.selectById(vo.getId());
		if (null == Project) {
			throw new ServiceException("2001", "项目不存在");
		}
		BeanUtils.copyProperties(vo, Project);
		this.setUpdateInfo(Project);
		boolean result = this.updateById(Project);
		if (result) {
			initRedisProject();
		}
		return result;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean deleteTwbProjectById(String id) {
		boolean result = this.removeById(id);
		if (result) {
			initRedisProject();
		}
		return result;
	}

	@Override
	public List<ListTwbProjectDto> listTwbProject(ListTwbProjectVo vo) {

		return twbProjectMapper.listTwbProject(vo);
	}

	@Override
	public List<TwbProject> searchProjectsByLoginUser(BaseLoginUser loginUser) {
		TwbPageUtil.orderBy("t.pro_seq asc");
		return twbProjectMapper.searchProjectsByLoginUser(loginUser.getId());
	}
}
